package com.xyzq.cgate.core.filter;

import com.xyzq.cgate.common.response.ResponseEntity;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

/**
 * @Description:
 * @author chenchen
 * @date 2019/6/19 15:08
 */
public abstract class AbstractFilter implements WebFilter {


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        final ServerHttpResponse response = exchange.getResponse();
        response.getHeaders().setContentType(MediaType.APPLICATION_JSON_UTF8);
        return doFilter(exchange, chain)
                .switchIfEmpty(Mono.just(false))
                .flatMap(result -> result ? chain.filter(exchange) : doDenyResponse(exchange));

    }

    protected abstract Mono<Boolean> doFilter(ServerWebExchange exchange, WebFilterChain chain);

    protected abstract Mono<Void> doDenyResponse(ServerWebExchange exchange);
}
